// compiler用来解析指令和插值表达式
class Compiler {
  constructor(vm) {
    this.el = vm.$el
    this.vm = vm
    // 希望立即编译模板
    this.compile(this.el)
  }
  // 编译模板，处理文本节点和元素节点
  compile (el) {
    let childNodes = el.childNodes // 是伪数组
    Array.from(childNodes).forEach(node => {
      console.log(':::compile.start')
      console.log('node.nodeType', node.nodeType)
      console.dir(node)
      console.log(':::compile.end')
      if (this.isTextNode(node)) {
        console.log('node.textContent', node.textContent)
        this.compileText(node)
      } else if (this.isElementNode(node)) {
        console.log('compileElement', node.attributes)
        this.compileElement(node)
      }

      // 判断node是否有子节点，如果有需要递归调用遍历子节点
      if (node.childNodes && node.childNodes.length) {
        this.compile(node)
      }
    })
  }
  // 编译元素节点，处理指令
  compileElement (node) {
    // 遍历所有的属性节点
    Array.from(node.attributes).forEach(attr => {
      // 判断是否有指令
      let attrName = attr.name
      if (this.isDirective(attrName)) {
        // v-text ==> text
        attrName = attrName.substr(2)
        console.log('attrName', attrName)
        let key = attr.value
        this.update(node, key, attrName)
      }
    })
  }
  update (node, key, attrName) {
    console.log('attrName', attrName)
    attrName = attrName.split(':')
    if(attrName[1]) {
      this.eventFn = attrName[1]
    }
    let updateFn = this[attrName[0] + 'Update']
    updateFn && updateFn.call(this, node, key, this.vm[key]) // 调用.call改成我们需要指向的实例
  }
  // 处理v-text指令
  textUpdate (node, key, value) {
    node.textContent = value

    // 创建watcher对象(数据改变更新视图)
    new Watch(this.vm, key, (newValue) => {
      console.log('watcher callback textUpdate', newValue)
      node.textContent = newValue
    })
  }
  // 处理v-html指令---ok
  htmlUpdate (node, key, value) {
    console.log('htmlUpdate',value, key, node.value )
    // node.value = value

    // 创建watcher对象(数据改变更新视图)
    new Watch(this.vm, key, (newValue) => {
      console.log('watcher callback htmlUpdate', newValue)
      // node.value = newValue
    })

    node.innerHTML = key



  }
  // 处理v-on指令
  onUpdate (node, key, value) {
    // node.value = value
    console.log('eventFn', this.eventFn, key, this.vm,node.value, key )
    let fn = this.eventFn
    if(!fn) {
      return
    }
    let handleFn = this.vm.$options.methods
    console.log('handleFn', handleFn, handleFn[key])

    // 创建watcher对象(数据改变更新视图)
    new Watch(this.vm, key, (newValue) => {
      console.log('watcher callback onUpdate', newValue)
      // node.value()
    })

    node.addEventListener(fn, () => {
      // console.log('addEventListener', fn, node.value)
      // node.value()
      handleFn[key]()
    });

  }
  // 处理v-model指令
  modelUpdate (node, key, value) {
    node.value = value

    // 创建watcher对象(数据改变更新视图)
    new Watch(this.vm, key, (newValue) => {
      console.log('watcher callback modelUpdate', newValue)
      node.value = newValue
    })

    // 实现双向绑定(页面发生该改变 对应的数据也应该发生变化)
    node.addEventListener('input', () => {
      this.vm[key] = node.value
    })
  }
  // 编译文本节点，处理插值表达式
  compileText (node) {
    // console.dir(node)
    // {{ msg }}
    let reg = /\{\{(.+?)\}\}/ //匹配插值表达式
    let value = node.textContent
    // console.log('node.textContent', node.textContent)
    // console.log('reg.test(value)', reg.test(value))
    if (reg.test(value)) {
      let key = RegExp.$1.trim() //获取第一个匹配的项
      node.textContent = value.replace(reg, this.vm[key])

      // 创建watcher对象(数据改变更新视图)
      new Watch(this.vm, key, (newValue) => {
        console.log('watcher callback compileText', newValue)
        node.textContent = newValue
      })

    }
  }
  // 判断元素属性是否是指令
  isDirective (attrName) {
    return attrName.startsWith('v-')
  }
  // 判断元素是不是文本节点
  isTextNode (node) {
    return node.nodeType === 3
  }
  // 判断元素是不是元素节点
  isElementNode (node) {
    return node.nodeType === 1
  }
}